/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package epaper;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.util.PDFMergerUtility;

/**
 *
 * @author Puneet
 */
public class MyPDFUtility {
    public static void mergeFiles(List<String> filesToBeMerged,
            String mergedFileLocation) {
        PDFMergerUtility pdfmu = new PDFMergerUtility();
        for (String s : filesToBeMerged) {
            pdfmu.addSource(s);
        }
        pdfmu.setDestinationFileName(mergedFileLocation);
        try {
            pdfmu.mergeDocuments();
        } catch (IOException ex) {
            Logger.getLogger(HT.class.getName()).log(Level.SEVERE, null, ex);
        } catch (COSVisitorException ex) {
            Logger.getLogger(HT.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void mergeStreams(List<InputStream> streamsToBeMerged,
            String mergedFileLocation) {
        System.out.println("Number of files : "+streamsToBeMerged.size());
        PDFMergerUtility pdfmu = new PDFMergerUtility();
        for (InputStream s : streamsToBeMerged) {
            pdfmu.addSource(s);
        }
        pdfmu.setDestinationFileName(mergedFileLocation);
        try {
            pdfmu.mergeDocuments();
        } catch (IOException ex) {
            Logger.getLogger(HT.class.getName()).log(Level.SEVERE, null, ex);
        } catch (COSVisitorException ex) {
            Logger.getLogger(HT.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void deleteMyFiles(List<String> filesToBeDeleted) {
        for (String str : filesToBeDeleted) {
            File f = new File(str);
            if (f.exists() && f.canWrite() && !f.isDirectory()) {
                f.delete();
            } else {
                System.out.println("" + f.exists() + " " + f.canWrite() + " " + f.isDirectory());
            }
        }
    }
}
